package itsm.isperp.framework.data.mybatis.type;

import itsm.isperp.framework.utils.HibernateUUIDGenerator;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

/**
 * UUID类型的操作类（数据库主键），使用有序的UUID。{@link UUIDUtils}
 * 
 * @author lizx
 * @date 2013-2-8
 * @version V1.0
 */
public class UUIDTypeHandler extends BaseTypeHandler<Serializable> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i,
			Serializable parameter, JdbcType jdbcType) throws SQLException {
		ps.setString(i, parameter.toString());
	}

	@Override
	public Serializable getNullableResult(ResultSet rs, String columnName)
			throws SQLException {
		return HibernateUUIDGenerator.generate();
	}

	@Override
	public Serializable getNullableResult(ResultSet rs, int columnIndex)
			throws SQLException {
		return HibernateUUIDGenerator.generate();
	}

	@Override
	public Serializable getNullableResult(CallableStatement cs, int columnIndex)
			throws SQLException {
		return HibernateUUIDGenerator.generate();
	}

}
